package com.shenganyuan.smac.msg;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.oklib.base.CoreBaseFragment;
import com.oklib.utils.helper.RxUtil;
import com.oklib.utils.network.http.ViseHttp;
import com.oklib.utils.network.http.callback.ACallback;
import com.oklib.utils.network.http.subscriber.ApiCallbackSubscriber;
import com.oklib.utils.view.SnackbarUtil;
import com.oklib.widget.recyclerview.BaseQuickAdapter;
import com.oklib.widget.recyclerview.BaseViewHolder;
import com.oklib.widget.recyclerview.CoreRecyclerView;
import com.oklib.widget.recyclerview.listener.OnItemClickListener;
import com.shenganyuan.smac.App;
import com.shenganyuan.smac.R;
import com.shenganyuan.smac.common.beans.ApiMessage;
import com.shenganyuan.smac.common.beans.Msg;
import com.shenganyuan.smac.common.beans.User;
import com.shenganyuan.smac.common.data.Api;
import com.shenganyuan.smac.common.utils.DataConverter;
import com.shenganyuan.smac.common.view.RecyclerViewItemDivider;
import com.shenganyuan.smac.msg.model.BeanChatTo;

import java.util.List;

import butterknife.BindView;

/**
 * @author Damon
 * @date 2017/12/14
 */

public class FrgMainMsg extends CoreBaseFragment {
    @BindView(R.id.main_msg_recycler)
    CoreRecyclerView recycler;

    public static FrgMainMsg newInstance() {
        Bundle args = new Bundle();
        FrgMainMsg fragment = new FrgMainMsg();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.main_frg_message;
    }

    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {
        recycler.init(new BaseQuickAdapter<Msg, BaseViewHolder>(R.layout.act_msg_item) {
            @Override
            protected void convert(BaseViewHolder helper, Msg item) {
                helper.setText(R.id.tv_name, item.fbname);
                helper.setText(R.id.tv_msg, item.nr);
                helper.setText(R.id.tv_time, DataConverter.getDateStr(item.fbTime));
            }
        });
        recycler.getRecyclerView().addItemDecoration(new RecyclerViewItemDivider(mContext, LinearLayoutManager.VERTICAL)
                .setAppearance(1, "#eeeeee"));
        recycler.addOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                BeanChatTo beanChatTo = new BeanChatTo();
                Msg msg = ((Msg) recycler.getAdapter().getItem(position));
                beanChatTo.id = msg.fbUserid;
                beanChatTo.name = msg.fbname;
                bundle.putSerializable("beanChatTo", beanChatTo);
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(getContext(), ActMsgChat.class);
                startActivity(intent);
            }
        });
        recycler.openRefresh(new CoreRecyclerView.addDataListener() {
            @Override
            public void addData(int page) {
                getDataFromServer();
            }
        });
    }

    @Override
    public void initData() {
    }

    private void getDataFromServer() {
        User user = App.getCurrentUser();
        ViseHttp.RETROFIT().create(Api.class)
                .getMainMsgs(user.getId(), App.getAccessToken())
                .compose(RxUtil.rxSchedulerHelper()).subscribe(
                new ApiCallbackSubscriber<>(new ACallback<ApiMessage>() {
                    @Override
                    public void onSuccess(ApiMessage data) {
                        try {
                            List<Msg> messages = data.getMessage();
                            if (messages != null && messages.size() > 0) {
                                recycler.getAdapter().setNewData(messages);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        SnackbarUtil.showShort(getView(), "获取消息失败");
                    }
                }));
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onResume() {
        super.onResume();
        getDataFromServer();
    }

    /**
     * 刷新页面
     */
    public void refreshPage() {
        getDataFromServer();
    }
}
